package com.housing.service.impl;

import com.housing.domain.bo.HouseBo;
import com.housing.service.IHouseService;
import com.zmetaport.common.core.utils.MapstructUtils;
import com.zmetaport.common.core.utils.StringUtils;
import com.zmetaport.common.mybatis.core.page.TableDataInfo;
import com.zmetaport.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.housing.domain.bo.BuildingBo;
import com.housing.domain.vo.BuildingVo;
import com.housing.domain.Building;
import com.housing.mapper.BuildingMapper;
import com.housing.service.IBuildingService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author chensm
 * @date 2024-12-15
 */
@RequiredArgsConstructor
@Service
public class BuildingServiceImpl implements IBuildingService {

    private final BuildingMapper baseMapper;

    private final IHouseService houseService;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public BuildingVo queryById(Long id){
        BuildingVo buildingVo = baseMapper.selectVoById(id);
        return buildingVo;
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<BuildingVo> queryPageList(BuildingBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Building> lqw = buildQueryWrapper(bo);
        Page<BuildingVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<BuildingVo> queryList(BuildingBo bo) {
        LambdaQueryWrapper<Building> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Building> buildQueryWrapper(BuildingBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Building> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getGroupId() != null, Building::getGroupId, bo.getGroupId());
        lqw.like(StringUtils.isNotBlank(bo.getGroupName()), Building::getGroupName, bo.getGroupName());
        lqw.like(StringUtils.isNotBlank(bo.getName()), Building::getName, bo.getName());
        lqw.eq(bo.getFloors() != null, Building::getFloors, bo.getFloors());
        lqw.eq(bo.getRoomsPerFloor() != null, Building::getRoomsPerFloor, bo.getRoomsPerFloor());
        lqw.eq(StringUtils.isNotBlank(bo.getRoomPrefix()), Building::getRoomPrefix, bo.getRoomPrefix());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(BuildingBo bo) {
        Building add = MapstructUtils.convert(bo, Building.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }

        List<HouseBo> houseBoList = new ArrayList<>();
        for (int i = 1; i <= bo.getFloors(); i++) {
            for (int j = 1; j <= bo.getRoomsPerFloor(); j++) {
                String roomNumber = bo.getRoomPrefix() + String.format("%02d", i) + String.format("%02d", j);
                HouseBo houseBo = HouseBo.builder().roomNumber(roomNumber).ownerId(bo.getOwnerId()).buildingId(add.getId()).status(1L).build();
                houseBoList.add(houseBo);
            }
        }
        houseService.insertByBos(houseBoList);
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(BuildingBo bo) {
        Building update = MapstructUtils.convert(bo, Building.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Building entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
